#!/bin/python

#
#Xithyl, Test Driven Development for C
#Copyright (C) 2012  Stepan Henek
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from .. import logger

import logging

logger.addLogger(__name__)

def __raise(fatal):
    if fatal:
        raise Exception("Test Failed")

def numbersLower(number1, number2, Logger=__name__, fatal=False):
    """
    Compares two numbers and return true if the first is lower then the second.

    @param number1 first number
    @param number2 second number
    @param Logger the logger where to store the result.
    @param fatal if true throw an exception when the condition doesn't match

    @return True if the first number is lower than the second.
    """

    result = isinstance(number1, (long, int)) and isinstance(number2, (long, int))
    if not result:
        logger.log("Number Lower: not numbers (%s, %s)" % (str(number1), str(number2)), Logger, logging.ERROR)
        __raise(fatal)
        return False

    #Are the numbers equal
    result = number1 < number2
    if not result:
        logger.log("Number Lower: not lower (%s < %s)" % (str(number1), str(number2)), Logger, logging.ERROR)
        __raise(fatal)
        return False

    logger.log("Number Lower: lower (%s < %s)" % (str(number1), str(number2)), Logger)
    return True

def numbersHigher(number1, number2, Logger=__name__, fatal=False):
    """
    Compares two numbers and return true if the first is higher then the second.

    @param number1 first number
    @param number2 second number
    @param Logger the logger where to store the result.
    @param fatal if true throw an exception when the condition doesn't match

    @return True if the first number is higher than the second.
    """

    result = isinstance(number1, (long, int)) and isinstance(number2, (long, int))
    if not result:
        logger.log("Number Higher: not numbers (%s, %s)" % (str(number1), str(number2)), Logger, logging.ERROR)
        __raise(fatal)
        return False

    #Are the numbers equal
    result = number1 > number2
    if not result:
        logger.log("Number Higher: not higher (%s > %s)" % (str(number1), str(number2)), Logger, logging.ERROR)
        __raise(fatal)
        return False

    logger.log("Number Higher: higher (%s > %s)" % (str(number1), str(number2)), Logger)
    return True

def numbersEquals(number1, number2, Logger=__name__, fatal=False):
    """
    Compares two numbers and return true if the numbers are the same.

    @param number1 first number
    @param number2 second number
    @param Logger the logger where to store the result.
    @param fatal if true throw an exception when the condition doesn't match

    @return True if the first number is the same as the second.
    """

    # Check whether the both numbers are numbers
    result = isinstance(number1, (long, int)) and isinstance(number2, (long, int))
    if not result:
        logger.log("Number Equals: not numbers (%s, %s)" % (str(number1), str(number2)), Logger, logging.ERROR)
        __raise(fatal)
        return False

    #Are the numbers equal
    result = number1 == number2
    if not result:
        logger.log("Number Equals: not equal (%s, %s)" % (str(number1), str(number2)), Logger, logging.ERROR)
        __raise(fatal)
        return False

    logger.log("Number Equals: are equal (%s, %s)" % (str(number1), str(number2)), Logger)
    return True

def arrayEqualsLen(arr1, arr2, Len, Logger=__name__, fatal=False):
    """
    Compares two arrays and return true if the numbers are the same.

    @param arr1 the first array
    @param arr2 the second array
    @param Len the length of the comparison
    @param Logger the logger where to store the result.
    @param fatal if true throw an exception when the condition doesn't match

    @return True if the first array is the same as the second.
    """

    # Check for lists
    result = isinstance(arr1, list) and isinstance(arr2, list)
    if not result:
        logger.log("ArraysLen Equals: not lists (%s, %s)" % (str(arr1), str(arr2)), Logger, logging.ERROR)
        __raise(fatal)
        return False

    # Check sufficient Length
    result = len(arr1) >= Len and len(arr2) >= Len
    if not result:
        logger.log("ArraysLen Equals: insufficient length (%s, %s, %d)" % (str(arr1), str(arr2), Len), Logger, logging.ERROR)
        __raise(fatal)
        return False

    for i in range(Len):
        if arr1[i] != arr2[i]:
            logger.log("ArraysLen Equals: not equal (%s, %s, \"%s\" != \"%s\")" % (str(arr1), str(arr2), arr1[i], arr2[i]), Logger, logging.ERROR)

            __raise(fatal)
            return False

    logger.log("ArraysLen Equals: are equal (%s, %s, %d)" % (str(arr1), str(arr2), Len), Logger)

    return True

def stringEqual(string1, string2, Logger=__name__, fatal=False):
    """
    Compares two strings and return true if the strings are the same.

    @param string1 first string
    @param string2 second string
    @param Logger the logger where to store the result.
    @param fatal if true throw an exception when the condition doesn't match

    @return True if the strings are equal
    """

    result = isinstance(string1, (str)) and isinstance(string2, (str))
    if not result:
        logger.log("String Equal: not strings (%s, %s)" % (str(string1), str(string2)), Logger, logging.ERROR)
        __raise(fatal)
        return False

    #Are the numbers equal
    result = string1 == string2
    if not result:
        logger.log("String Equal: not equal (\"%s\" = \"%s\")" % (string1, string2), Logger, logging.ERROR)
        __raise(fatal)
        return False

    logger.log("String Equal: equal (\"%s\" = \"%s\")" % (string1, string2), Logger)
    return True
